package org.nighthawkhosting.SC2Mapster;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MapView extends ListActivity {
	private ArrayList<Map> mapList = null;
	private MapAdapter adapter;

	private Runnable fillDataRunnable;
	private ProgressDialog progress;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forumlist);

		fillDataRunnable = new Runnable(){
			@Override
			public void run() {
				fillData();
			}
		};
		
		Thread thread =  new Thread(null, fillDataRunnable, "MagentoBackground");
		thread.start();
		registerForContextMenu(getListView());
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		//super.onCreateContextMenu(menu, v, menuInfo);
		//menu.add(0, DELETE_ID, 0, R.string.menu_delete);
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
		Long place = new Long(info.id);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(adapter.getItem(place.intValue()).getMapLink());
		AlertDialog alert = builder.create();
		alert.setCanceledOnTouchOutside(true);
		alert.show();
	}

	private void fillData() {
		handler.sendEmptyMessage(0);
		try {
			MapParser parser = new MapParser();
			parser.setURL("http://www.sc2mapster.com/maps.rss");
			mapList = parser.parse();
			handler.sendEmptyMessage(2);
			Thread.sleep(100);
		} catch (Exception e) {
			Log.e("BACKGROUND_PROC", e.getMessage());
		}
		handler.sendEmptyMessage(1);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				progress = ProgressDialog.show(MapView.this, "", "Retrieving data ...", true);
			} else if (msg.what == 1) {
				progress.dismiss();
			} else if (msg.what == 2) {
				adapter = new MapAdapter(MapView.this, R.layout.forumrow, mapList);
				setListAdapter(adapter);
				adapter.notifyDataSetChanged();
			}
			//pd.dismiss();
			//tv.setText(pi_string);
		}
	};

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent viewIntent = new Intent("android.intent.action.VIEW",
				Uri.parse(mapList.get(position).getMapLink()));  
		startActivity(viewIntent);    
//
//		if(forumList.get(position).isForumSuper())
//			return;
//
//		Intent i = new Intent(this, ThreadView.class);
//		i.putExtra("_title", forumList.get(position).getForumTitle());
//		i.putExtra("_url", "http://forums.sc2mapster.com" + forumList.get(position).getForumURL());
//
//		Bundle cookieBundle = new Bundle();
//		for (int i2 = 0; i2 < cookieData.size(); i2++) {			
//			cookieBundle.putStringArrayList(String.valueOf(i2), CookieParser.parse(cookieData.get(i2)));
//		}
//
//		i.putExtra("_cookies", cookieBundle);
//		//this.startActivity(i);
//		startActivityForResult(i, 1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, Menu.FIRST, 0, "Refresh");
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		case Menu.FIRST:
			Thread thread =  new Thread(null, fillDataRunnable, "MagentoBackground");
			thread.start();
			//fillDataThread.start();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	public class MapAdapter extends ArrayAdapter<Map> {

		private ArrayList<Map> maps;

		public MapAdapter(Context context, int textViewResourceId, ArrayList<Map> items) {
			super(context, textViewResourceId, items);
			this.maps = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Map data = maps.get(position);
			View v = convertView;

			if (data == null) return v;

				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.maprow, null);

				TextView title = (TextView) v.findViewById(R.id.maptitle);
				
				title.setText(data.getMapTitle());

				return v;
		}
	}
}